<%@page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<html>
<head>
    <base href="<%=basePath%>">
    <title>调查结果</title>
    <meta charset="UTF-8">
</head>
<body>
<%@include file="top.html"%>

<%
    // 1. 设置请求编码（必须放在最前面！）
    request.setCharacterEncoding("UTF-8");

// 2. 从session获取用户信息
    String namesex = (String) session.getAttribute("namesex");
    if (namesex == null) {
        namesex = "用户";
    }

// 3. 获取用户选择的爱好（简化逻辑）
    String[] hobbyNames = {"影视欣赏", "阅读书籍", "体育运动", "户外旅游", "流行音乐"};
    List<String> selectedHobbies = new ArrayList<>();

    for (int i = 0; i < 5; i++) {
        String hobbyValue = request.getParameter("hobby" + i);
        if (hobbyValue != null) {
            selectedHobbies.add(hobbyValue); // 直接使用原始值

// 更新统计
            synchronized (application) {
                Integer count = (Integer) application.getAttribute("hobby" + i);
                application.setAttribute("hobby" + i, (count == null) ? 1 : count + 1);
            }
        }
    }
%>

<!-- 4. 显示结果 -->
<h3><%= namesex %>，你的业余爱好如下：</h3>

<% if (selectedHobbies.isEmpty()) { %>
<p>您未选择任何爱好</p >
<% } else {
    for (String hobby : selectedHobbies) { %>
<p><%= hobby %></p >
<% }
} %>

<h3>全局统计结果：</h3>
<% for (int i = 0; i < hobbyNames.length; i++) {
    Integer count = (Integer) application.getAttribute("hobby" + i);
    if (count == null) count = 0; %>
<p>选择 <%= hobbyNames[i] %> 的人次为：<%= count %></p >
<% } %>

</body>
</html>